package com.summer.data.util;

import com.summer.data.annotation.Engine;
import com.summer.data.annotation.Table;
import com.summer.data.annotation.column.Charset;
import com.summer.data.annotation.column.Collate;
import com.summer.data.type.Tuple2;

/**
 * @author 345480567@qq.com
 */
public class TableUtils {
    public static String getTableName(Class<?> clazz) {

        Table tableName = clazz.getAnnotation(Table.class);
        if (tableName == null) {
            return null;
        }
        return tableName.value();
    }

    public static Tuple2<Engine> findBestEngine(Engine[] data, Class<?> clazz) {
        Tuple2<Engine> tuple2 = new Tuple2<>();
        for (Engine item : data) {
            Class<?> dialectType = item.type();
            if (dialectType == clazz) {
                tuple2.t1 = item;
                break;
            }
            if (dialectType == null) {
                tuple2.t2 = item;
            }
        }
        return tuple2;
    }

    public static Tuple2<Charset> findBestCharset(Charset[] data, Class<?> clazz) {
        Tuple2<Charset> tuple2 = new Tuple2<>();
        for (Charset item : data) {
            Class<?> dialectType = item.type();
            if (dialectType == clazz) {
                tuple2.t1 = item;
                break;
            }
            if (dialectType == null) {
                tuple2.t2 = item;
            }
        }
        return tuple2;
    }

    public static Tuple2<Collate> findBestCollate(Collate[] data, Class<?> clazz) {
        Tuple2<Collate> tuple2 = new Tuple2<>();
        for (Collate item : data) {
            Class<?> dialectType = item.type();
            if (dialectType == clazz) {
                tuple2.t1 = item;
                break;
            }
            if (dialectType == null) {
                tuple2.t2 = item;
            }
        }
        return tuple2;
    }

}
